package com.kevis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class DataMigrationService {
    private final JdbcTemplate mssqlJdbcTemplate;
    private final JdbcTemplate mysqlJdbcTemplate;

    @Autowired
    public DataMigrationService(JdbcTemplate mssqlJdbcTemplate, JdbcTemplate mysqlJdbcTemplate) {
        this.mssqlJdbcTemplate = mssqlJdbcTemplate;
        this.mysqlJdbcTemplate = mysqlJdbcTemplate;
    }

    public void migrateData() {
        System.out.println(mssqlJdbcTemplate);
        System.out.println(mysqlJdbcTemplate);

        System.out.println(mssqlJdbcTemplate.getDataSource());
        System.out.println(mysqlJdbcTemplate.getDataSource());
        // 从MSSQL中选择数据
        List<Map<String, Object>> data = mssqlJdbcTemplate.queryForList("SELECT TOP 10000 * FROM [DB_Base].[dbo].[BtxCMS_News]");

        // 将数据插入到MySQL中
        for (Map<String, Object> row : data) {
            String sql = "INSERT INTO t_article (title,summary,content) VALUES (?,?,?)";
            mysqlJdbcTemplate.update(sql, row.get("Title"),row.get("Descriptions"),row.get("Contents"));
        }
    }
}
